package org.blog.service;

import java.util.List;

import org.blog.dao.CatalogDao;
import org.blog.helper.Result;
import org.blog.model.ActionType;
import org.blog.model.ModelCatalog;
import org.blog.model.ModelPassport;
import org.blog.service.util.ServiceConstants;
import org.blog.service.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CatalogServiceImpl implements CatalogService {

	@Autowired
	private CatalogDao catalogDao;

	@Override
	public Result<?> addCatalog(ModelCatalog t, ModelPassport passport) {

		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (ValidateHelper.validate(t, ActionType.ADD)) {
				t.setAction(1);
				t.setActionTime(System.currentTimeMillis() / 1000);
				t.setCreateTime(System.currentTimeMillis() / 1000);
				catalogDao.addCatalog(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<?> modifyCatalog(ModelCatalog t, ModelPassport passport) {

		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (ValidateHelper.validate(t, ActionType.MODIFY)) {
				t.setAction(2);
				t.setActionTime(System.currentTimeMillis() / 1000);
				catalogDao.modifyCatalog(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}

	}

	@Override
	public Result<?> deleteCatalog(ModelCatalog t, ModelPassport passport) {

		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {

			if (ValidateHelper.validate(t, ActionType.DELETE)) {
				t.setAction(3);
				t.setActionTime(System.currentTimeMillis() / 1000);
				catalogDao.deleteCatalog(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}

	}

	public Result<?> delete(ModelCatalog t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (ValidateHelper.validate(t, ActionType.DELETE)) {
				catalogDao.delete(t.getId());
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<?> getCatalog(ModelCatalog t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (ValidateHelper.validate(t, ActionType.GET)) {
				ModelCatalog catalog = catalogDao.getCatalog(t.getId());
				return new Result<ModelCatalog>(catalog, 200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<?> getListCatalog(ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			List<ModelCatalog> list = catalogDao.getListCatalog();
			return new Result<Object>(list, 200);
		} else {
			return new Result<Object>(401);
		}
	}
}
